<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简历分析结果 - AI简历分析系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        :root {
            --primary-color: #5C7CFA;
            --secondary-color: #F8F9FA;
            --text-color: #212529;
            --text-muted: #6C757D;
            --border-color: #DEE2E6;
            --card-bg: #FFFFFF;
            --warning-color: #DC3545;
            --success-color: #28A745;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: var(--secondary-color);
            color: var(--text-color);
            line-height: 1.6;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }

        .container-main {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 1rem;
        }

        .header {
            background-color: var(--card-bg);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
            padding: 1.5rem 0;
            margin-bottom: 2rem;
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header h1 {
            color: var(--primary-color);
            margin: 0;
            font-size: 1.6rem;
            font-weight: 700;
        }

        .card {
            border: none;
            border-radius: 12px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
            margin-bottom: 2rem;
            background-color: var(--card-bg);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 25px rgba(0, 0, 0, 0.1);
        }

        .card-header {
            background-color: transparent;
            padding: 1.5rem 2rem 1rem;
            border-bottom: 1px solid var(--border-color);
            font-weight: 600;
            font-size: 1.25rem;
            color: var(--text-color);
        }

        .card-header .bi {
            color: var(--text-muted);
        }

        .card-body {
            padding: 1.5rem 2rem 2rem;
        }

        .preformatted {
            white-space: pre-wrap;
            font-family: 'Inter', sans-serif;
            line-height: 1.8;
            padding: 1.5rem;
            background-color: var(--secondary-color);
            border-radius: 8px;
            border: 1px solid #E9ECEF;
            color: #343A40;
            font-size: 0.95rem;
        }

        .chat-container {
            height: 650px;
            display: flex;
            flex-direction: column;
            border: 1px solid var(--border-color);
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
        }

        .chat-history {
            flex: 1;
            padding: 1.5rem;
            overflow-y: auto;
            background-color: #FDFDFD;
        }

        .chat-history::-webkit-scrollbar {
            width: 6px;
        }

        .chat-history::-webkit-scrollbar-thumb {
            background-color: rgba(67, 97, 238, 0.2);
            border-radius: 3px;
        }

        .chat-message {
            margin-bottom: 1rem;
            display: flex;
        }

        .user-message {
            justify-content: flex-end;
        }

        .ai-message {
            justify-content: flex-start;
        }

        .message-bubble {
            padding: 0.8rem 1.2rem;
            border-radius: 1.2rem;
            max-width: 80%;
            word-wrap: break-word;
            font-size: 0.95rem;
            line-height: 1.5;
        }

        .user-message .message-bubble {
            background-color: var(--primary-color);
            color: white;
            border-bottom-right-radius: 0.4rem;
        }

        .ai-message .message-bubble {
            background-color: #E9ECEF;
            color: var(--text-color);
            border-bottom-left-radius: 0.4rem;
        }

        .chat-input {
            display: flex;
            padding: 1rem 1.5rem;
            border-top: 1px solid var(--border-color);
            background-color: var(--card-bg);
            gap: 0.5rem;
        }

        .chat-input input {
            flex: 1;
            padding: 0.75rem 1.25rem;
            border: 1px solid var(--border-color);
            border-radius: 2rem;
            font-size: 0.95rem;
            transition: all 0.2s ease;
        }

        .chat-input input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(92, 124, 250, 0.25);
            outline: none;
        }

        .chat-input button {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            background-color: var(--primary-color);
            color: white;
            border: none;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        .chat-input button:hover {
            background-color: #4A63D9;
            transform: translateY(-2px);
        }

        .loading-dots {
            display: inline-flex;
            gap: 4px;
        }

        .loading-dots span {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background-color: #A9A9A9;
            animation: bounce 1.4s infinite ease-in-out both;
        }

        .loading-dots span:nth-child(1) {
            animation-delay: -0.32s;
        }

        .loading-dots span:nth-child(2) {
            animation-delay: -0.16s;
        }

        @keyframes bounce {
            0%, 80%, 100% { transform: scale(0); }
            40% { transform: scale(1); }
        }

        .btn-back {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 0.6rem 1.2rem;
            background-color: var(--card-bg);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            color: var(--text-muted);
            text-decoration: none;
            transition: all 0.3s ease;
        }

        .btn-back:hover {
            background-color: #F1F3F5;
            color: var(--text-color);
            border-color: #D2D6DC;
        }

        .d-flex.justify-content-between.align-items-center {
            flex-wrap: wrap;
        }
    </style>
</head>
<body>
<input type="hidden" id="selectedModel" th:value="${selectedModel}" />
<meta name="_csrf" th:content="${_csrf.token}" />
<meta name="_csrf_header" th:content="${_csrf.headerName}" />

<div class="header">
    <div class="container-main">
        <div class="d-flex justify-content-between align-items-center">
            <h1>AI 简历分析系统</h1>
            <div class="header-buttons">
                <a href="/interview" class="btn-back">
                    <i class="bi bi-arrow-left"></i> 返回重新分析
                </a>
            </div>
        </div>
    </div>
</div>

<div class="container-main mt-4">
    <div class="card">
        <div class="card-header">
            <i class="bi bi-bar-chart-line me-2"></i> 简历分析结果
        </div>
        <div class="card-body">
            <pre class="preformatted" th:text="${aiResponse}"></pre>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <i class="bi bi-question-circle me-2"></i> 面试问题建议
        </div>
        <div class="card-body">
            <pre class="preformatted" th:text="${interviewQuestions}"></pre>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <i class="bi bi-chat-square-text me-2"></i> AI 面试助手
        </div>
        <div class="chat-container">
            <div class="chat-history" id="chatHistory">
                <div class="ai-message chat-message">
                    <div class="message-bubble">
                        您好！我已完成简历分析，有任何关于面试或简历的问题都可以问我。
                    </div>
                </div>
            </div>
            <div class="chat-input">
                <input type="text" id="userQuestion" placeholder="请输入您的问题...">
                <button id="sendButton">
                    <i class="bi bi-paper-plane"></i>
                </button>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script th:inline="javascript">
    // 发送消息功能
    function sendMessage() {
        const userQuestionInput = document.getElementById('userQuestion');
        const question = userQuestionInput.value.trim();
        const chatHistory = document.getElementById('chatHistory');
        const selectedModel = document.getElementById('selectedModel').value;

        if (!question) return;

        // 添加用户消息到聊天记录
        const userMessageDiv = document.createElement('div');
        userMessageDiv.className = 'user-message chat-message';
        userMessageDiv.innerHTML = `<div class="message-bubble">${question}</div>`;
        chatHistory.appendChild(userMessageDiv);

        // 清空输入框
        userQuestionInput.value = '';
        userQuestionInput.disabled = true;

        // 添加加载中提示
        const loadingDiv = document.createElement('div');
        loadingDiv.className = 'ai-message chat-message';
        loadingDiv.innerHTML = `<div class="message-bubble"><div class="loading-dots"><span></span><span></span><span></span></div></div>`;
        chatHistory.appendChild(loadingDiv);

        // 滚动到底部
        chatHistory.scrollTop = chatHistory.scrollHeight;

        // 准备上下文（分析结果）
        const context = document.querySelector('.preformatted')?.textContent || '';

        // 获取CSRF令牌
        const csrfToken = document.querySelector('meta[name="_csrf"]').content;
        const csrfHeader = document.querySelector('meta[name="_csrf_header"]').content;

        // 发送请求到后端
        const formData = new FormData();
        formData.append('question', question);
        formData.append('context', context);
        formData.append('aiModel', selectedModel);

        const headers = new Headers();
        headers.append(csrfHeader, csrfToken);

        fetch('/chat', {
            method: 'POST',
            headers: headers,
            body: formData
        })
        .then(response => {
            if (!response.ok) {
                return response.text().then(errorText => {
                    throw new Error(`请求失败: ${response.status} - ${errorText}`);
                });
            }
            return response.text();
        })
        .then(aiResponse => {
            // 移除加载提示
            chatHistory.removeChild(loadingDiv);

            // 添加AI回复
            const aiMessageDiv = document.createElement('div');
            aiMessageDiv.className = 'ai-message chat-message';
            aiMessageDiv.innerHTML = `<div class="message-bubble">${aiResponse}</div>`;
            chatHistory.appendChild(aiMessageDiv);

            // 滚动到底部
            chatHistory.scrollTop = chatHistory.scrollHeight;
            userQuestionInput.disabled = false;
            userQuestionInput.focus();
        })
        .catch(error => {
            // 移除加载提示
            chatHistory.removeChild(loadingDiv);

            // 显示错误消息
            const errorDiv = document.createElement('div');
            errorDiv.className = 'ai-message chat-message';
            errorDiv.innerHTML = `<div class="message-bubble" style="color: var(--warning-color);"><strong>错误：</strong> ${error.message}</div>`;
            chatHistory.appendChild(errorDiv);

            // 滚动到底部
            chatHistory.scrollTop = chatHistory.scrollHeight;
            userQuestionInput.disabled = false;
            userQuestionInput.focus();
        });
    }

    // 绑定回车键发送消息
    document.getElementById('userQuestion').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });

    // 绑定发送按钮点击事件
    document.getElementById('sendButton').addEventListener('click', sendMessage);
</script>
</body>
</html>